package com.asiainfo_linkage.scheduler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.asiainfo_linkage.model.RemTask;
import com.asiainfo_linkage.service.RemTaskMemberService;
import com.asiainfo_linkage.service.RemTaskService;

/**
 * 任务管理器。
 * @author Administrator
 */
@IocBean(create="scheduleAllTask")
public class TaskManager{
	
	@Inject 
	protected RemTaskService remTaskService;	
	
	@Inject 
	protected RemTaskMemberService remTaskMemberService;
	
	protected Map<Long,Scheduler > schedulerMap = new HashMap<Long, Scheduler>();

	public void scheduleAllTask(){
		List<RemTask> list = remTaskService.queryEnabledTask(null);
		if(list!=null && list.size()>0){
			for(RemTask task:list){
				scheduleTask(task);
			}
		}
	}
	
	public void scheduleTask(RemTask task){
		NotifierTimerTask notifierTimerTask = new NotifierTimerTask(task, task.getMembers());
		OneTimeScheduler oneTimeScheduler = new OneTimeScheduler();
		schedulerMap.put(task.getTask_id(), oneTimeScheduler);
		//如果是单次提醒任务。
		if(task.getRem_type().equals("0")){
			//安排定时任务。
			oneTimeScheduler.schedule(notifierTimerTask);
			remTaskService.statusUpdate(task);
		}
	}

}
